home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / LIB211.ZIP;1 / TIME.PRG < prev    next >
Encoding:
Text File  |  1993-11-19  |  10.0 KB  |  262 lines

  1. *----------------------------------------------------------------------
  2. *-- Program...: TIME.PRG
  3. *-- Programmer: Ken Mayer (CIS: 71333,1030)
  4. *-- Date......: 04/19/1993
  5. *-- Notes.....: These are a series of routines that deal with time
  6. *--             strings, and so on. Very useful. See README.TXT for more
  7. *--             details on the use of this library file.
  8. *-----------------------------------------------------------------------
  9.  
  10. FUNCTION Delay
  11. *-----------------------------------------------------------------------
  12. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  13. *-- Date........: 03/01/1992
  14. *-- Notes.......: Delay Loop.  Returns .T. after lapse of given number
  15. *--               of seconds.  Accurate to one tick, about 1/18.2 secs.
  16. *-- Written for.: dBASE IV, Version 2.0 or higher
  17. *-- Rev. History: 03/01/1992 -- Original function
  18. *--               04/20/1993 -- modified for dBASE IV 2.0 syntax and a
  19. *--               bug fixed, Jay Parsons
  20. *-- Calls.......: TIME2SEC()           Function in TIME.PRG
  21. *-- Called by...: Any
  22. *-- Usage.......: Delay(<nSeconds>)
  23. *-- Example.....: lX= Delay(10.25)
  24. *-- Returns.....: Logical
  25. *-- Parameters..: nSeconds = number of seconds to delay
  26. *-----------------------------------------------------------------------
  27.  
  28.    parameters nSeconds         && up to 86400, one day
  29.    private nTimeout, nTimenow, lRollover
  30.  
  31.    m->nTimeout = 100 * ( Time2Sec( time( 0 ) ) + m->nSeconds )
  32.    if m->nTimeout > 8640000
  33.       m->lRollover = .T.
  34.       m->nTimeout = m->nTimeout - 8640000
  35.    else
  36.       m->lRollover = .F.
  37.    endif
  38.    do while .T.
  39.       m->nTimenow = 100 * Time2Sec( time( 0 ) )
  40.       if m->nTimenow < m->nTimeout
  41.          m->lRollover = .F.
  42.       else
  43.          if .not. m->lRollover
  44.             exit
  45.          endif
  46.       endif
  47.    enddo
  48.  
  49. RETURN .T.
  50. *-- EoF: Delay()
  51.  
  52. FUNCTION Time2Sec
  53. *-----------------------------------------------------------------------
  54. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  55. *-- Date........: 03/01/1992
  56. *-- Notes.......: Convert HH:MM:SS or HH:MM:SS.SS string to seconds.
  57. *-- Written for.: dBASE IV
  58. *-- Rev. History: 03/01/1992 -- Original
  59. *-- Calls.......: None
  60. *-- Called by...: Any
  61. *-- Usage.......: Time2Sec("<cTime>")
  62. *-- Example.....: ?Time2Sec("01:24:15")
  63. *-- Returns.....: Numeric
  64. *-- Parameters..: cTime = Time string in format HH:MM:SS or HH:MM:SS.SS
  65. *-----------------------------------------------------------------------
  66.    
  67.    parameters cTime
  68.    private cTemp, nSecs
  69.  
  70.    m->cTemp = cTime
  71.    m->nSecs = 3600 * val( m->cTemp )
  72.    m->cTemp = substr( m->cTemp, at( ":", m->cTemp ) + 1 )
  73.    m->nSecs = m->nSecs + 60 * val( m->cTemp )
  74.    
  75. RETURN m->nSecs + val( substr( m->cTemp, at( ":", m->cTemp ) + 1 ) )
  76. *-- EoF: Time2Sec()
  77.  
  78. FUNCTION Sec2Time
  79. *-----------------------------------------------------------------------
  80. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  81. *-- Date........: 03/01/1992
  82. *-- Notes.......: Convert number of seconds to time string in format of
  83. *--               HH:MM:SS or HH:MM:SS.SS.
  84. *-- Written for.: dBASE IV
  85. *-- Rev. History: 03/01/1992 -- Original 
  86. *-- Calls.......: None
  87. *-- Called by...: Any
  88. *-- Usage.......: Sec2Time("<cTime>")
  89. *-- Example.....: ? Sec2Time(30001.3)
  90. *-- Returns.....: Character String
  91. *-- Parameters..: nSeconds = Seconds to be converted 
  92. *-----------------------------------------------------------------------
  93.  
  94.    parameters nSeconds
  95.    private nHrs, nMins, nSecs, cTemp
  96.  
  97.    m->nSecs = mod( nSeconds, 86400 )
  98.    m->nHrs  = int( m->nSecs / 3600 )
  99.    m->nSecs = m->nSecs - m->nHrs * 3600
  100.    m->nMins = int( m->nSecs / 60 )
  101.    m->nSecs = m->nSecs - m->nMins * 60
  102.    m->cTemp = transform( m->nHrs, "@L 99" ) + ":" ;
  103.               + transform( m->nMins, "@L 99" ) + ":"
  104.  
  105. RETURN m->cTemp + iif( m->nSecs = int( m->nSecs ), ;
  106.                        transform( m->nSecs, "@L 99" ), ;
  107.                        transform( m->nSecs, "@L 99.99" ) )
  108. *-- EoF: Sec2Time()
  109.  
  110. FUNCTION DiffTime
  111. *-----------------------------------------------------------------------
  112. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  113. *-- Date........: 03/01/1992
  114. *-- Notes.......: Calculate difference between two times given as
  115. *--               HH:MM:SS or HH:MM:SS.SS strings.  If second time is
  116. *--               smaller than first, assumes midnight passage. Returns
  117. *--               HH:MM:SS string, or HH:MM:SS.SS string if fractional
  118. *--               seconds passed.
  119. *-- Written for.: dBASE IV
  120. *-- Rev. History: 03/01/1992 -- Original
  121. *-- Calls.......: TIME2SEC()           Function in TIME.PRG
  122. *--               SEC2TIME()           Function in TIME.PRG
  123. *-- Called by...: Any
  124. *-- Usage.......: DiffTime("<cTime1>","<cTime2>")
  125. *-- Example.....: ?DiffTime("2:03:24","5:12:33")
  126. *-- Returns.....: Character String
  127. *-- Parameters..: cTime1 = Time to subtract from cTime2
  128. *--               cTime2 = Time to subtract from (larger value, unless
  129. *--                            after midnite)
  130. *-----------------------------------------------------------------------
  131.  
  132.    parameters cTime1, cTime2
  133.  
  134. RETURN Sec2Time( 86400 + Time2Sec( cTime2 ) - Time2Sec( cTime1 ) )
  135. *-- EoF: DiffTime()
  136.  
  137. FUNCTION Civ2Mil
  138. *-----------------------------------------------------------------------
  139. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  140. *-- Date........: 03/01/1992
  141. *-- Notes.......: Converts string like "12:59 a.m." to standard 24-hour
  142. *--               HH:MM:SS. If the string contains neither "a" nor "p",
  143. *--               the hours will not be converted.
  144. *-- Written for.: dBASE IV
  145. *-- Rev. History: 03/01/1992 -- Original 
  146. *-- Calls.......: None
  147. *-- Called by...: Any
  148. *-- Usage.......: Civ2Mil("<cCivilTime>")
  149. *-- Example.....: ?Civ2Mil("2:03:24 a.m.")
  150. *-- Returns.....: Character String
  151. *-- Parameters..: cCivilTime = Time to convert to 24 hour time.
  152. *-----------------------------------------------------------------------
  153.  
  154.    parameters cCiviltime
  155.    private cTstring, nTime
  156.  
  157.    m->cTstring =  trim( lower( cCiviltime ) )
  158.    m->nTime = val( m->cTstring )
  159.    do case
  160.       case "p" $ m->cTstring
  161.          m->cTstring = left( m->cTstring, ;
  162.                        at( "p", m->cTstring ) - 1 )
  163.          m->nTime = mod( m->nTime, 12 ) + 12
  164.       case "a" $ m->cTstring
  165.          m->cTstring = left( m->cTstring, ;
  166.                        at( "a", m->cTstring ) - 1 )
  167.          m->nTime = mod( m->nTime, 12 )
  168.    endcase
  169.    m->cTstring = transform( m->nTime, "@L 99" ) ;
  170.                  + trim( substr( m->cTstring, at( ":", m->cTstring )))
  171.    
  172. RETURN m->cTstring + iif( len( m->cTstring ) = 5, ":00", "" )
  173. *-- EoF: Civ2Mil()
  174.  
  175. FUNCTION Mil2Civ
  176. *-----------------------------------------------------------------------
  177. *-- Programmer..: Jay Parsons (CIS: 72662,1302)
  178. *-- Date........: 03/01/1992
  179. *-- Notes.......: Converts HH:MM:SS 24-hour string to a.m./p.m. notation
  180. *-- Written for.: dBASE IV
  181. *-- Rev. History: 03/01/1992 -- Original 
  182. *-- Calls.......: None
  183. *-- Called by...: Any
  184. *-- Usage.......: Mil2Civ("<cMilTime>")
  185. *-- Example.....: ?Mil2Civ("14:03:24")
  186. *-- Returns.....: Character String
  187. *-- Parameters..: cMilTime = Time to convert to 24 hour time.
  188. *-----------------------------------------------------------------------
  189.  
  190.    parameters cMiltime
  191.    private cCivtime, nHours, cMins
  192.  
  193.    m->cCivtime = ltrim( trim( cMiltime ) )
  194.    m->nHours = val( m->cCivtime )
  195.    m->cMins = substr( m->cCivtime, at( ":", m->cCivtime ) ) + " " ;
  196.               + iif( m->nHours > 11, "p.m.", "a.m." )
  197.    
  198. RETURN ltrim( str( mod( m->nHours + 11, 12 ) + 1 ) ) + m->cMins
  199. *-- EoF: Mil2Civ()
  200.  
  201. FUNCTION IsAmPm
  202. *-----------------------------------------------------------------------
  203. *-- Programmer..: Charles Miedzinksi (Borland Technical Support)
  204. *-- Date........: 11/05/1992
  205. *-- Notes.......: Taken from TechNotes (?), Checks to see if a character
  206. *--               string is in proper AM/PM time format.
  207. *-- Written for.: dBASE IV 1.1, 1.5
  208. *-- Rev. History: 11/05/1992 -- Modified a bit for the Library ...
  209. *-- Calls.......: None
  210. *-- Called by...: Any
  211. *-- Usage.......: IsAmPm(<cTime>)
  212. *-- Example.....: ?IsAmPm("20:15:05")   && should return .F.
  213. *-- Returns.....: Logical
  214. *-- Parameters..: cTime = a time string in format: "HH:MM:SS a/pm"
  215. *--                       Seconds part of string is optional, as is A/PM
  216. *-----------------------------------------------------------------------
  217.    
  218.    parameters cTime
  219.  
  220. RETURN iif(val(left(cTime, 2)) >= 1 .and. val(left(cTime, 2)) <= 12, ;
  221.        iif(val(substr(cTime, 4, 2)) >= 0 ;
  222.        .and. val(substr(cTime, 4, 2)) <= 59, ;
  223.        iif(substr(cTime, 6, 1) $ 'aApP', .T., ;
  224.        iif(val(substr(cTimer, 6, 2)) >= 0 ;
  225.        .and. val(substr(cTime, 6, 2)) <= 59, ;
  226.        .T., .F.)), .F.), .F.)
  227. *-- EoF: IsAmPm()
  228.  
  229. FUNCTION AddTimes
  230. *-----------------------------------------------------------------------
  231. *-- Programmer..: Angus Scott-Fleming (CIS: 75500,3223)
  232. *-- Date........: 05/28/1992
  233. *-- Notes.......: Add two times
  234. *-- Written for.: dBASE IV
  235. *-- Rev. History: 05/28/1992 -- Original
  236. *-- Calls.......: Time2Sec               Function in TIME.PRG
  237. *--               Sec2Time               Function in TIME.PRG
  238. *-- Called by...: Any
  239. *-- Usage.......: AddTimes("<cTime1>","<cTime2>") 
  240. *--                OR 
  241. *--               AddTimes("<cTime1>",<nHours>)
  242. *-- Example.....: ?AddTimes("01:24:15","02:00:00") 
  243. *--                    OR 
  244. *--               ?AddTimes("01:24:15",2.5)
  245. *-- Returns.....: cTime    = Time string in format HH:MM:SS/HH:MM:SS.SS
  246. *-- Parameters..: cTime1,2 = Time string in format HH:MM:SS/HH:MM:SS.SS
  247. *--               nHours   = Numeric (number of hours to add to cTime1
  248. *-----------------------------------------------------------------------
  249.  
  250.    parameters Time_One,Time_Two
  251.  
  252.    if type("Time_Two") = "N"
  253.       RETURN Sec2Time(Time2Sec(Time_One)+Time_Two*3600)
  254.    endif
  255.  
  256. RETURN Sec2Time(Time2Sec(Time_One)+Time2Sec(Time_Two))
  257. *-- EoF: AddTimes()
  258.  
  259. *-------------------------------------------------------------------------------
  260. *-- EoP: TIME.PRG
  261. *-------------------------------------------------------------------------------
  262.